import {Md5} from 'ts-md5/dist/md5';

export const getMd5 = (str: any) => {
    return (Md5.hashStr(str) + "").toUpperCase();
};

//排版无线层级路由
export const sort = (parent_obj: any, allList: Array<any>) => {
    let child_list = new Array<any>();
    for (let i = 0; i < allList.length; i++) {
        let now_obj = allList[i];
        let now_pid: string = now_obj["pid"];
        //    查得到
        if (now_pid.toString() == (parent_obj["id"] + "")) {
            child_list.push(now_obj);//同级别添加
            sort(now_obj, allList);//继续递归
        }
        //不同级别不做处理
        else {
        }
        if (child_list.length != 0) {
            parent_obj["children"] = child_list;
        }
    }
    return parent_obj
};

//排版无线层级路由
export const getTree = (list: any) => {
    //根节点数组
    let genArray = new Array<any>();
    //非根节点数组
    let childArray = new Array<any>();

    for (let key in list) {
        if (list[key]["pid"] == "0") {
            genArray.push(list[key]);
        } else {
            childArray.push(list[key]);
        }
    }
    //非根节点数组
    let routerArray = new Array<any>();
    for (let key in genArray) {
        routerArray.push(sort(genArray[key], childArray));
    }
    return JSON.stringify(routerArray)
};

export const isBlank = (obj: any): boolean => {
    if (obj == null || obj == undefined) {
        return true;
    }
    return obj.toString().trim() == "";

};

export const isNotBlank = (obj: any): boolean => {
    return (!isBlank(obj))
};

export const getUUID = () => {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
};

export const getUUIDStr = () => {
    return myReplace(getUUID(), "-", "");
};

export const myReplace = (content: any, old: any, new_str: any) => {
    let reg = new RegExp(old, "g"); //创建正则RegExp对象
    return content.replace(reg, new_str);
};

export const menuTreeToArray = (list: any, newArr: any, parentPath: any = null) => {
    list.forEach((item: any) => {
        item.parentPath=parentPath;
        const {children} = item;
        if (children) {
            delete item.children;
            if (children.length) {
                newArr.push(item);
                return menuTreeToArray(children, newArr, item.path);
            }
        }
        newArr.push(item);
    });
    return newArr;
};
/**
 * 排序
 */
export const compareProps = (key: any) => {
    return (obj1: any, obj2: any) => {
        const val1 = obj1[key];
        const val2 = obj2[key];
        if (val1 < val2) {
            return -1;
        }
        if (val1 > val2) {
            return 1;
        }
        return 0;
    };
};






